import productOrder from '@/components/product-item/components/product-order.vue'
import { formatDate } from '@/utils/date';
import { listOrder,deleteOrder,cancelOrder } from '@/api/order.js';

export default {
	components: {
		productOrder
	},
	data() {
		return {
			tabCurrentIndex: 0,
			orderParam: {
				status: -1,
				pageNum: 0,
				pageSize: 5
			},
			orderList: [],
			loadingType:'more',
			navList: [{
					state: -1,
					text: '全部'
				},
				{
					state: 0,
					text: '待付款'
				},
				{
					state: 1,
					text: '已完成'
				},
				{
					state: 2,
					text: '已取消'
				}
			],
		};
	},
	onLoad(options) {
		this.tabCurrentIndex = +options.state;
		this.loadData();
	},
	filters: {
		formatStatus(status) {
			let statusTip = '';
			switch (+status) {
				case 0:
					statusTip = '等待付款';
					break;
				case 1:
					statusTip = '交易完成';
					break;
				case 2:
					statusTip = '交易关闭';
					break;
			}
			return statusTip;
		},
		formatDateTime(time) {
			if (time == null || time === '') {
				return 'N/A';
			}
			let date = new Date(time);
			return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
		},
	},
	methods: {
		//获取订单列表
		loadData(type='refresh') {
			if(type=='refresh'){
				this.orderParam.pageNum=0;
			}else{
				this.orderParam.pageNum++;
			}
			//这里是将订单挂载到tab列表下
			let index = this.tabCurrentIndex;
			let navItem = this.navList[index];
			let state = navItem.state;
			if (this.loadingType === 'loading') {
				//防止重复加载
				return;
			}
			this.orderParam.status = navItem.state;
			this.loadingType = 'loading';
			listOrder(this.orderParam).then(response => {
				let pageData = response.data;
				
				if(type=='refresh'){
					this.orderList = pageData.content;
				}else{
					//在原先list中追加数据
					this.orderList = this.orderList.concat(pageData.content);
				}
				if(pageData.last){
					//没有更多了
					this.loadingType = 'nomore';
				}else{
					this.loadingType = 'more';
				}
			});
		},
		//swiper 切换
		changeTab(e) {
			this.tabCurrentIndex = e.target.current;
			this.loadData();
		},
		//顶部tab点击
		tabClick(index) {
			this.tabCurrentIndex = index;
		},
		//删除订单
		deleteOrder(orderSn) {
			let _this = this;
			uni.showModal({
				title: '提示',
				content: '是否要删除该订单？',
				success: function (res) {
					if (res.confirm) {
						uni.showLoading({
							title: '请稍后'
						});
						let data = {
							orderSn : orderSn
						};
						deleteOrder(data).then(response=>{
							uni.hideLoading();
							_this.loadData();
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		//取消订单
		cancelOrder(orderSn) {
			let _this = this;
			uni.showModal({
				title: '提示',
				content: '是否要取消该订单？',
				success: function (res) {
					if (res.confirm) {
						uni.showLoading({
							title: '请稍后'
						});
						let data = {
							orderSn : orderSn
						};
						cancelOrder(data).then(response=>{
							uni.hideLoading();
							_this.loadData();
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		//支付订单
		payOrder(orderSn){
			uni.redirectTo({
				url: `/pages/order/pay/pay?orderSn=${orderSn}`
			});
		},
		//查看订单详情
		showOrderDetail(orderSn){
			uni.navigateTo({
				url: `/pages/order/detail/detail?orderSn=${orderSn}`
			})
		},
		//查看订单详情
		showRes(orderSn){
			uni.navigateTo({
				url: `/pages/order/res/res?orderSn=${orderSn}`
			})
		},
		//计算商品总数量
		calcTotalNumber(item){
			let totalNumber = 0;
			if(item.orderItemEntityList!=null && item.orderItemEntityList.length>0){
				for(let item of item.orderItemEntityList){
					totalNumber+=item.number
				}
			}
			return totalNumber;
		},
	},
}